package com.hr.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hr.dto.Page;
import com.hr.entity.Role;
import com.hr.entity.User;
import com.hr.entity.UserRole;
import com.hr.mapper.RoleMapper;
import com.hr.mapper.UserMapper;
import com.hr.mapper.UserRoleMapper;
import com.hr.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private RoleMapper roleMapper;

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
        // 新增角色
        List<Role> roleList = user.getRoleList();
        for (Role role : roleList){
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getUserId());
            userRole.setRoleId(role.getRoleId());
            userRole.setStatus(1);
            userRoleMapper.addUserRole(userRole);
        }
    }

    @Override
    public void deleteUser(Integer userId) {
        userMapper.deleteUser(userId);
        // 禁用该用户的角色
        userRoleMapper.disableUserRole(userId);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);

        List<Role> roleList = user.getRoleList();
        if(!CollectionUtils.isEmpty(roleList)){
            // 删除原角色
            userRoleMapper.deleteUserRole(user.getUserId());
            // 新增新角色
            for (Role role : roleList){
                UserRole userRole = new UserRole();
                userRole.setUserId(user.getUserId());
                userRole.setRoleId(role.getRoleId());
                userRole.setStatus(1);
                userRoleMapper.addUserRole(userRole);
            }
        }
    }

    @Override
    public Page getUserPage(int pageNo, int pageSize) {
        Page page = new Page();
        PageHelper.startPage(pageNo, pageSize);
        List<User> userList = userMapper.getUserList();
        PageInfo<User> info = new PageInfo<>(userList);

        // 查询角色
        for (User user : userList){
            //查询角色
            List<UserRole> userRoleList = userRoleMapper.getUserRoleList(user.getUserId());

            List<Role> roleList = new ArrayList<>();
            for (UserRole userRole : userRoleList){
                Role role = roleMapper.getRole(userRole.getRoleId());
                roleList.add(role);
            }
            user.setRoleList(roleList);
        }

        page.setPage(info.getList());
        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setPageCount(info.getPages());
        page.setTotal(info.getTotal());
        page.setHasPrePage(info.isHasPreviousPage());
        page.setHasNextPage(info.isHasNextPage());
        return page;
    }

    @Override
    public User getUser(User user) {
        User u = userMapper.getUser(user);
        // 查询角色
        List<UserRole> userRoleList = userRoleMapper.getUserRoleList(u.getUserId());

        List<Role> roleList = new ArrayList<>();
        for (UserRole userRole : userRoleList){
            Role role = roleMapper.getRole(userRole.getRoleId());
            roleList.add(role);
        }
        u.setRoleList(roleList);
        return u;
    }
}
